<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>固定导航栏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            height: 3000px;
        }
        
        img {
            width: 100%;
        }
        
        #nav {
            position: absolute;
            width: 100%;
            height: 50px;
            background-color: black;
        }
        
        ul {
            list-style-type: none;
            margin-left: 90px;
        }
        
        li {
            float: left;
            color: #fff;
            font: 15px "微软雅黑";
            margin-left: 20px;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div id="top">
        <img src="image/9-1-top.png">
    </div>

    <div id="nav">
        <ul>
            <li>体育首页</li>
            <li>中国足球</li>
            <li>亚冠</li>
            <li>中超</li>
            <li>国足</li>
            <li>中甲</li>
            <li>视频直播</li>
        </ul>
    </div>
    <div id="main"></div>
    <script>
        function scroll() {
            if (window.pageYOffset !== undefined) {
                return {
                    left: window.pageXOffset,
                    top: window.pageYOffset
                }
            } else if (document.compatMode === 'CSS1Compat') {
                return {
                    left: document.documentElement.scrollLeft,
                    top: document.documentElement.scrollTop
                }
            }
            return {
                left: document.body.scrollLeft,
                top: document.body.scrollTop
            }
        }
        window.onload = function() {
            var nav = document.getElementById('nav')
            var top = document.getElementById('top')
            var topHeight = top.offsetHeight
            var navTop = nav.offsetHeight
            window.onscroll = function() {
                if (scroll().top >= topHeight) {
                    nav.style.top = 0 + 'px'
                    nav.style.position = 'fixed'
                } else {
                    nav.style.top = topHeight + 'px'
                    nav.style.position = 'absolute'
                }
            }
        }
    </script>
</body>

</html>